package com.lxj.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtils {
	/**
	 * 正则验证手机号码
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean validateMoblie(String phone) {
		int l = phone.length();
		boolean rs = false;
		switch (l) {
		case 7:
			if (matchingText("^(13[0-9]|15[0-9]|18[7|8|9|6|5])\\d{4}$", phone)) {
				rs = true;
			}
			break;
		case 11:
			if (matchingText("^(13[0-9]|15[0-9]|18[7|8|9|6|5])\\d{4,8}$", phone)) {
				rs = true;
			}
			break;
		default:
			rs = false;
			break;
		}
		return rs;
	}

	private static boolean matchingText(String expression, String text) {
		Pattern p = Pattern.compile(expression); // 正则表达式
		Matcher m = p.matcher(text); // 操作的字符串
		boolean b = m.matches();
		return b;
	}

	/**
	 * 电子邮件验证
	 * 
	 * @return
	 */
	public static boolean validataEmail(String email) {
		String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern regex = Pattern.compile(check);
		Matcher matcher = regex.matcher(email);
		return matcher.matches();
	}

	/**
	 * 验证QQ号码是否正确
	 * 
	 * @param qq
	 * @return
	 */
	public static boolean validataQQ(String qq){
		String rule="[1-9][0-9]{4,14}";//正则表达式的验证规则。  
		return qq.matches(rule);  
	}
}
